﻿@{
    ViewBag.Title = "Web Application";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section style{
    <style type="text/css">
        .my-menu a.x-menu-item {
            padding-left: 0;
        }
    </style>
}
@section script
{
    <script type="text/javascript">
        function addTab(tabPanel, id, url, title, cookie, last) {
            if (cookie) {
            } else
                saveTabIntoCookie(id, url, title);

            var tab = tabPanel.getComponent(id);
            if (!tab)
                tab = tabPanel.add({
                    id: id,
                    title: title,
                    closable: true,
                    loader: {
                        url: getTabUrl(url),
                        renderer: "frame",
                        loadMask: {
                            showMask: false,
                            msg: "Loading " + title + "..."
                        }
                    },
                    listeners: {
                        close: removeTab
                    }
                });
            if (cookie != true || last)
                tabPanel.setActiveTab(tab);
        }

        function removeTab(item) {
            var userConfig = window.Ext.util.Cookies.get('userConfig');
            if (userConfig || userConfig == null) {
            } else
                initialUserConfig();
            var config = window.Ext.decode(userConfig);
            if (config.user == window.userId) {
                for (var i = 0; i < config.tabs.length; i++)
                    if (config.tabs[i].id == item.id)
                        config.tabs.splice(i, 1);
                initialUserConfig(config);
            } else
                initialUserConfig();
        }

        function saveTabIntoCookie(id, url, title) {
            var userConfig = window.Ext.util.Cookies.get('userConfig');
            if (userConfig) {
            } else
                initialUserConfig();
            var config = window.Ext.decode(userConfig);
            if (config.user == window.userId) {
                config.tabs.push({ id: id, url: url, title: title });
                initialUserConfig(config);
            } else
                initialUserConfig();
        }

        function restorePreviousState() {
            var userConfig = window.Ext.util.Cookies.get('userConfig');
            if (userConfig) {
                var config = window.Ext.decode(userConfig);
                if (config.user == window.userId) {
                    for (var i = 0; i < config.tabs.length; i++)
                        addTab(window.App.TabPanel1, config.tabs[i].id, config.tabs[i].url, config.tabs[i].title, true, (i + 1) == config.tabs.length);
                    return;
                }
            }
            initialUserConfig();
        }

        function initialUserConfig(value) {
            if (value) {
            } else
                value = { user: window.userId, tabs: [] };
            window.Ext.util.Cookies.set('userConfig', window.Ext.encode(value));
        }

        function getTabUrl(data) {
            var s = data.split(',');
            if (s.length > 0)
                if (s[0] == 1)
                    return '@Url.Action("Index",
                                        "Role")';
                else if (s[0] == 2)
                    return '@Url.Action("Index",
                                        "Permission")';
                else if (s[0] == 3)
                    return '@Url.Action("Index",
                                        "UserGroup")';
                else if (s[0] == 4)
                    return '@Url.Action("Index",
                                        "User")';
            return '';
        }

    </script>
}
@(Html.X()
      .Viewport()
      .Listeners(c => c.AfterRender.Handler = "restorePreviousState();")
      .RenderTo("body")
      .Layout(LayoutType.Border)
      .Items(view =>
                 {
                     view.Add(Html.X()
                                  .Panel()
                                  .Border(false)
                                  .Region(Region.North)
                                  .Items(topPanel =>
                                             {
                                                 topPanel.Add(Html.X()
                                                                  .Toolbar()
                                                                  .Border(false)
                                                                  .Items(topMenu =>
                                                                             {
                                                                                 topMenu.Add(Html.X()
                                                                                                 .Button()
                                                                                                 .Text("Management")
                                                                                                 .Menu(menu =>
                                                                                                       {
                                                                                                           
                                                                                                       }));
                                                                                 topMenu.Add(Html.X()
                                                                                                 .Button()
                                                                                                 .Text("General")
                                                                                                 .Menu(menu =>
                                                                                                       menu.Add(Html.X()
                                                                                                                    .Menu()
                                                                                                                    .Items(general => general.Add(Html.X()
                                                                                                                                                      .MenuItem()
                                                                                                                                                      .Text("Security")
                                                                                                                                                      .Icon(Icon.Application)
                                                                                                                                                      .Menu(Html.X()
                                                                                                                                                                .Menu()
                                                                                                                                                                .Items(v =>
                                                                                                                                                                           {
                                                                                                                                                                               v.Add(Html.X()
                                                                                                                                                                                         .MenuItem()
                                                                                                                                                                                         .Text("Role")
                                                                                                                                                                                         .Icon(Icon.ApplicationGo)
                                                                                                                                                                                         .Listeners(b =>
                                                                                                                                                                                                        {
                                                                                                                                                                                                            b.Click.Handler = "addTab(#{TabPanel1},'idRole','1', 'Role')";
                                                                                                                                                                                                        }));
                                                                                                                                                                               v.Add(Html.X()
                                                                                                                                                                                         .MenuItem()
                                                                                                                                                                                         .Text("Permission")
                                                                                                                                                                                         .Icon(Icon.ApplicationGo)
                                                                                                                                                                                         .Listeners(b =>
                                                                                                                                                                                                        {
                                                                                                                                                                                                            b.Click.Handler = "addTab(#{TabPanel1},'idPermission','2', 'Permission')";
                                                                                                                                                                                                        }));
                                                                                                                                                                               v.Add(Html.X()
                                                                                                                                                                                         .MenuSeparator());
                                                                                                                                                                               v.Add(Html.X()
                                                                                                                                                                                         .MenuItem()
                                                                                                                                                                                         .Text("Usergroup")
                                                                                                                                                                                         .Icon(Icon.ApplicationGo)
                                                                                                                                                                                         .Listeners(b =>
                                                                                                                                                                                                        {
                                                                                                                                                                                                            b.Click.Handler = "addTab(#{TabPanel1},'idUsergroup','3', 'Usergroup')";
                                                                                                                                                                                                        }));
                                                                                                                                                                               v.Add(Html.X()
                                                                                                                                                                                         .MenuItem()
                                                                                                                                                                                         .Text("User")
                                                                                                                                                                                         .Icon(Icon.ApplicationGo)
                                                                                                                                                                                         .Listeners(b =>
                                                                                                                                                                                                        {
                                                                                                                                                                                                            b.Click.Handler = "addTab(#{TabPanel1},'idUser','4', 'User')";
                                                                                                                                                                                                        }));
                                                                                                                                                                           })))))));
                                                                                 topMenu.Add(Html.X()
                                                                                                 .ToolbarFill());
                                                                                 topMenu.Add(Html.X()
                                                                                                 .Label()
                                                                                                 .Text("Current " + User.Identity.Name)
                                                                                                 .StyleSpec("font-weight:bold"));
                                                                                 topMenu.Add(Html.X()
                                                                                                 .Label()
                                                                                                 .Text("|")
                                                                                                 .MarginSpec("0 5"))
                                                                                         ;
                                                                                 topMenu.Add(Html.X()
                                                                                                 .Button()
                                                                                                 .Text("Log off")
                                                                                                 .Listeners(v =>
                                                                                                                {
                                                                                                                    v.Click.Handler = "window.location.href = '" + Url.Action("Logoff",
                                                                                                                                                                              "Account") + "'";
                                                                                                                }));
                                                                             }));
                                             }));
                     view.Add(Html.X()
                                  .TabPanel()
                                  .ID("TabPanel1")
                                  .Region(Region.Center)
                                  .BodyCls("tab-body"));
                 }))